package com.dataseek.iot.core.util;

import javax.swing.text.Document;
import java.lang.reflect.Field;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * @author cc
 * @Title: 数据工具类
 * @Description: 数据工具类
 * @date 2021/10/303:04 下午
 * @since 1.0
 */
public class DataUtil {

    /**
     * 数据行转列
     * @param clazz
     * @param list
     * @param <T>
     * @return
     */
    public static  <T> List convert(Class<T> clazz, List<T> list){
        List<Map> result;
        Field[] fields = clazz.getDeclaredFields();
        result = new ArrayList<>(fields.length);
        String fieldName = null;
        for (int i = 0; i < fields.length; i++) {
            fieldName = fields[i].getName();
            Map m = new HashMap();
            m.put(fieldName, new LinkedList<>());
            result.add(m);
        }
        for (T t : list) {
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                field.setAccessible(true);
                fieldName = field.getName();
                Map m = result.get(i);
                List l = (LinkedList) m.get(fieldName);
                try {
                    l.add(field.get(t));
                    m.put(fieldName, l);
                    result.add(m);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * 通过反射实例化对象
     * @param clazz
     * @param map
     * @param <T>
     * @return
     */
    public static <T> Object getReflection(String clazz, List<Map<String,Object>> map){
        Object o = null;
        try {
            o = Class.forName(clazz).newInstance();
            Class clz = Class.forName(clazz).newInstance().getClass();
            for (Field f : clz.getDeclaredFields()){
                for(Map m : map){
                    String name = f.getName();
                    if(m.containsKey(name)){
                        f = clz.getDeclaredField(name);
                        f.setAccessible(true);
                        f.set(o, m.get(name));
                    }
                }
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        return o;
    }

    /**
     * 生成指定位数的随机数
     * @param length
     * @return
     */
    public static String getRandomId(int length){
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            val += String.valueOf(random.nextInt(10));
        }
        return val;

    }

    /**
     * MD5加密
     * @param plainText
     * @return
     */
    public static String stringToMD5(String plainText) {
        byte[] secretBytes = null;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(
                    plainText.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("没有这个md5算法！");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }


}
